package com.wolfking.demo.nio.file;

import java.io.ByteArrayOutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;

public class FileToByte {
    public static void main(String[] args) throws Exception {
        FileChannel readChannel = FileChannel.open(Paths.get("C:\\ssl_key\\ssl.log"), StandardOpenOption.READ);
        System.out.println(readChannel.size());
        int k;
        byte[] bytes = new byte[1024];
        ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        while ((k = readChannel.read(byteBuffer)) != -1) {
            byteBuffer.flip();
            byteBuffer.get(bytes, 0, k);
            byteBuffer.clear();
            bos.write(bytes, 0, k);
        }
        System.out.println(bos.toByteArray().length);
        readChannel.close();
    }
}
